package qs.openxt.libs.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONObject;

public class JSONUtil {
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public static JSONObject toJSON(com.alibaba.fastjson.JSONObject obj) {
		try{
			JSONObject RE = new JSONObject();
			Iterator it = obj.keySet().iterator();
			while (it.hasNext()) {
				Object o = it.next();
				if(o instanceof String){
					String key = (String)o;
					Object value = obj.get(key);
					if(value instanceof Date){
						RE.put(key, dateFormat.format(value));
					}else if(value instanceof com.alibaba.fastjson.JSONObject){
						JSONObject re = toJSON((com.alibaba.fastjson.JSONObject)value);
						if(re != null)
							RE.put(key, re);
					}else if(value instanceof com.alibaba.fastjson.JSONArray){
						JSONArray re = toJSON((com.alibaba.fastjson.JSONArray)value);
						if(re != null)
							RE.put(key, re);
					}else
						RE.put(key, value);
				}
			}
			return RE;
		}catch(Exception e){
			return null;
		}
	}
	
	public static JSONArray toJSON(com.alibaba.fastjson.JSONArray array) {
		try{
			JSONArray RE = new JSONArray();
			int len = array.size();
			for (int i = 0; i < len; i++) {
				Object value = array.get(i);
				
				if(value instanceof Date){
					RE.put(dateFormat.format(value));
				}else if(value instanceof com.alibaba.fastjson.JSONObject){
					JSONObject re = toJSON((com.alibaba.fastjson.JSONObject)value);
					if(re != null)
						RE.put(re);
				}else if(value instanceof com.alibaba.fastjson.JSONArray){
					JSONArray re = toJSON((com.alibaba.fastjson.JSONArray)value);
					if(re != null)
						RE.put(re);
				}else
					RE.put(value);
			}
			return RE;
		}catch(Exception e){
			return null;
		}
	}
	
	public static com.alibaba.fastjson.JSONObject toJSON(JSONObject obj) {
		if(obj == null)
			return null;
		try{
			com.alibaba.fastjson.JSONObject RE = new com.alibaba.fastjson.JSONObject();
			Iterator it = obj.keys();
			while (it.hasNext()) {
				Object o = it.next();
				if(o instanceof String){
					String key = (String)o;
					Object value = obj.opt(key);
					if(value instanceof Date){
						RE.put(key, dateFormat.format(value));
					}else if(value instanceof JSONObject){
						com.alibaba.fastjson.JSONObject re = toJSON((JSONObject)value);
						if(re != null)
							RE.put(key, re);
					}else if(value instanceof JSONArray){
						com.alibaba.fastjson.JSONArray re = toJSON((JSONArray)value);
						if(re != null)
							RE.put(key, re);
					}else
						RE.put(key, value);
				}
			}
			return RE;
		}catch(Exception e){
			return null;
		}
	}
	
	public static com.alibaba.fastjson.JSONArray toJSON(JSONArray array) {
		try{
			com.alibaba.fastjson.JSONArray RE = new com.alibaba.fastjson.JSONArray();
			int len = array.length();
			for (int i = 0; i < len; i++) {
				Object value = array.get(i);
				
				if(value instanceof Date){
					RE.add(dateFormat.format(value));
				}else if(value instanceof JSONObject){
					com.alibaba.fastjson.JSONObject re = toJSON((JSONObject)value);
					if(re != null)
						RE.add(re);
				}else if(value instanceof JSONArray){
					com.alibaba.fastjson.JSONArray re = toJSON((JSONArray)value);
					if(re != null)
						RE.add(re);
				}else
					RE.add(value);
			}
			return RE;
		}catch(Exception e){
			return null;
		}
	}
}
